var ROOT;
var CURDIR;
var DENYLIST = [];

function Directory(name){
	this.type='directory';
	this.name = name;
	this.upper = null;
	this.child = [];
}

Directory.prototype.appendChild = function(obj){
	if(this.child.indexOf(obj.name) == -1){
		this.child.push(obj);
		obj.upper = this;
	}
}

Directory.prototype.deleteChild = function(name) {
	var index = this.child.indexOf(name);
	if(index >= 0){
		this.child.splice(index, 1);
	}
}

Directory.prototype.getChild = function(showhidden){
	return this.child.filter(function(obj){
		if(obj.name[0] == '.' && showhidden != true){
			return false;
		}else if(DENYLIST.indexOf(obj.name) >= 0){
			return false;
		}else{
			return true;
		}
	});
}

Directory.prototype.getChildByName = function(name){
	var filtered = this.child.filter(function(obj){
		return (obj.name==name)?true:false;
	});
	if(filtered.length == 0){
		return null;
	}else{
		return filtered[0];
	}
}

Directory.prototype.getPath = function(){
	if(CURDIR == ROOT){
		return '/';
	}
	var tmpdir = CURDIR;
	arr = [];
	while(tmpdir != null){
		arr.unshift(tmpdir.name);
		tmpdir = tmpdir.upper;
	}
	return arr.join('/');
}

Directory.prototype.getDir = function(dir){
	var curdir;
	if(dir == "/"){
		return ROOT;
	}else if(dir == ""){
		return this;
	}else if(dir[0] == '/'){
		curdir = ROOT;
		dir = dir.slice(1);
	}else{
		curdir = this;
	}

	//remove last slash
	if(dir[dir.length-1] == '/'){
		dir = dir.slice(0, dir.length-1);
	}
	var dirs = dir.split('/');
	for(var i=0; i<dirs.length; i++){
		dirname = dirs[i];
		if(dirname == ".."){
			curdir = curdir.upper;
		}else if(dirname == "."){
			curdir = curdir;
		}else{
			curdir = curdir.getChildByName(dirname);
		}

		if(curdir == null){
			return null;
		}else if(i < dirs.length-1 && curdir.type != 'directory'){
			return null;
		}
	}
	return curdir;
}

Directory.prototype.getAutocomplete = function(str){
	var dir = this.getDir(str);
	var path = str;
	var start = "";
	var isHidden = false;
	if(dir == null){
		var index = str.lastIndexOf('/');
		if(index == -1){
			path = "";
			start = str;
		}else{
			path = str.slice(0, index);
			start = str.slice(index+1);
		}
		dir = this.getDir(path);
	}
	if(dir == null || dir.type != 'directory'){
		return null;
	}

	if(path.slice(path.lastIndexOf('/')+1)  == '.'){
		path = str.slice(0, path.lastIndexOf('/')+1);
		start = '.';
	}

	if(start[0] == '.'){
		isHidden = true;
	}

	var arrComplete = dir.getChild(isHidden).filter(function(obj){
		return obj.name.indexOf(start)==0?true:false;
	}).map(function(obj){
		if(path == ""){
			return obj.name;
		}else{
			return path + (path[path.length-1]=='/'?'':'/') + obj.name;
		}
	});

	if(arrComplete.length == 0){
		return null;
	}else{
		return arrComplete;
	}
}

Directory.prototype.getSize = function(){
	return 4092;
}

function SpecialDirectory(name, dest){
	this.type='specialdirectory';
	this.name = name;
	this.upper = null;
	this.dest = dest;
}

SpecialDirectory.prototype.getSize = function(){
	return 4092;
}


function File(name){
	this.type = 'file';
	this.name = name;
	this.upper = null;
	this.content = null;
}

File.prototype.getSize = function(){
	return this.content.length;
}

function Program(name, func){
	this.type='program';
	this.name = name;
	this.upper = null;
	this.func = func;
}

Program.prototype.execute = function(args){
	this.func.call(args);
}

Program.prototype.getSize = function(){
	return this.func.toString().length;
}
